package com.order.service.impl;

import com.order.context.BaseContext;
import com.order.dto.CommentsDto;
import com.order.entity.Comments;
import com.order.entity.User;
import com.order.mapper.CommentsMapper;
import com.order.mapper.UserMapper;
import com.order.service.CommentsService;
import com.order.vo.CommentsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void insert(CommentsDto commentsDto) {
        User user = userMapper.getById(BaseContext.getCurrentId());
        Comments comments = Comments.builder()
                .productId(commentsDto.getProductId())
                .userId(BaseContext.getCurrentId())
                .username(user.getUsername())
                .comments(commentsDto.getComments())
                .build();
        commentsMapper.insert(comments);
    }

    @Override
    public List<CommentsVo> search(Long productId) {
        return commentsMapper.getByProductId(productId);
    }
}
